package com.centaline.androidsalesblog.activities.sale;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.SchoolAdapter;
import com.centaline.androidsalesblog.api.sale.SchoolListApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.DropListener;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.model.businissobject.SchoolBo;
import com.centaline.androidsalesblog.model.databaseobject.GscpDo;
import com.centaline.androidsalesblog.model.databaseobject.RegionDo;
import com.centaline.androidsalesblog.model.responseobject.SchoolListRo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.utils.DropUtil;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centaline.androidsalesblog.widgets.dropdown.DoubleDrop;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.centanet.framework.interfaces.IResponseListener;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;
import java.util.Locale;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 优选学区列表
 */
public class SchoolListActivity extends BaseActivity {

    private MRecyclerView mRecyclerView;

    private ArrayList<SchoolBo> schoolBos = new ArrayList<>();
    private int iRefreshType;

    private SchoolListApi schoolListApi;
    private String menuString;
    private DoubleDrop doubleDrop;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_school_list;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_school_list);
        mRecyclerView = (MRecyclerView) findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initViews() {
        doubleDrop = new DoubleDrop(findViewById(R.id.appbar), this);
        doubleDrop.setDropListener(new DropListener() {
            @Override
            public void dropComplete(boolean fromMore, int type, DropBo... dropBos) {
                int size = dropBos.length;
                switch (size) {
                    case 1:
                        if (DropUtil.compareValue(dropBos[0])) {
                            menuString = "区域";
                            schoolListApi.setGscopeLevel(0);
                        } else {
                            menuString = dropBos[0].getText();
                            schoolListApi.setGscopeLevel(2);
                            schoolListApi.setGscpid(dropBos[0].getValue());
                            baiduEvent("H1_3_Click", dropBos[0].getText());
                        }
                        invalidateOptionsMenu();
                        mRecyclerView.startRefresh();
                        break;
                    case 2:
                        if (DropUtil.compareValue(dropBos[1])) {
                            menuString = dropBos[0].getText();
                            schoolListApi.setGscopeLevel(2);
                            schoolListApi.setGscpid(dropBos[0].getValue());
                            baiduEvent("H1_3_Click", dropBos[0].getText());
                        } else {
                            menuString = dropBos[1].getText();
                            schoolListApi.setGscopeLevel(3);
                            schoolListApi.setGscpid(dropBos[1].getValue());
                            baiduEvent("H1_3_Click", dropBos[1].getText());
                        }
                        invalidateOptionsMenu();
                        mRecyclerView.startRefresh();
                        break;
                    default:
                        break;
                }
            }
        });
        mRecyclerView.setAdapter(new SchoolAdapter(this, GlideLoad.init(this), schoolBos));
        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                schoolListApi.setStartIndex(0);
                request();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                schoolListApi.setStartIndex(schoolBos.size());
                request();
            }

            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(SchoolListActivity.this, SchoolDetailActivity.class)
                        .putExtra(ExtraConstant.SchoolId, schoolBos.get(position).getSchoolID()));
            }
        });

        schoolListApi = new SchoolListApi(SchoolListActivity.class, SchoolListRo.class, new IResponseListener<SchoolListRo>() {
            @Override
            public void onResponse(SchoolListRo schoolListRo) {
                final ArrayList<SchoolBo> temp = schoolListRo.getList();
                if (iRefreshType == 1)
                    schoolBos.clear();
                if (temp == null) {
                    mRecyclerView.stopRefresh(false);
                } else {
                    schoolBos.addAll(schoolListRo.getList());
                    mRecyclerView.stopRefresh(schoolListRo.getList().size() == 10);
                }
                if (schoolListApi.getStartIndex() == 0) {
                    snack(schoolListRo.getTotal() == 0 ?
                            getString(R.string.snack_school_nothing) :
                            String.format(Locale.CHINA, getString(R.string.snack_school), schoolListRo.getTotal()));
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                mRecyclerView.refreshError();
                netWorkUnable();
            }
        });
        mRecyclerView.startRefresh();
        RxDataBase.getSaleRegions()
                .subscribeOn(Schedulers.io())
                .map(new Func1<ArrayList<RegionDo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<RegionDo> regionDos) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo1, dropBo2;
                        dropBo1 = new DropBo(10);
                        dropBo1.setText(CommonConstant.DROP_TEXT_NO_LIMITED);
                        dropBo1.setValue(CommonConstant.DROP_VALUE_NULL);
                        list.add(dropBo1);
                        for (RegionDo regionDo : regionDos) {
                            dropBo1 = new DropBo(10);
                            dropBo1.setText(regionDo.getC_distname());
                            dropBo1.setValue(regionDo.getScp_mkt());
                            ArrayList<DropBo> temp = new ArrayList<>();
                            dropBo2 = new DropBo(10);
                            dropBo2.setText(CommonConstant.DROP_TEXT_NO_LIMITED);
                            dropBo2.setValue(CommonConstant.DROP_VALUE_NULL);
                            temp.add(dropBo2);
                            final ArrayList<GscpDo> gscpDos = regionDo.getList();
                            for (GscpDo gscpDo : gscpDos) {
                                dropBo2 = new DropBo(10);
                                dropBo2.setText(gscpDo.getC_distname());
                                dropBo2.setValue(gscpDo.getGscp_id());
                                temp.add(dropBo2);
                            }
                            dropBo1.setChildrenList(temp);
                            list.add(dropBo1);
                        }
                        return list;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        doubleDrop.init(dropBos);
                    }
                });
        menuString = getString(R.string.menu_school_default);
    }

    private void request() {
        schoolListApi.startRequest();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_shcool, menu);
        MenuItem menuItem = menu.findItem(R.id.action_school);
        menuItem.setTitle(menuString);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_school) {
            doubleDrop.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
